package com.genntii.examManager.service.impl;

import com.genntii.examManager.common.utils.UUIDLong;
import com.genntii.examManager.domain.entity.Course;
import com.genntii.examManager.domain.entity.Exam;
import com.genntii.examManager.domain.entity.QuestionBank;
import com.genntii.examManager.domain.question.mongo.ExamDetail;
import com.genntii.examManager.domain.question.mongo.QuestionBankMongo;
import com.genntii.examManager.domain.question.mongo.TopicMongo;
import com.genntii.examManager.domain.question.vo.*;
import com.genntii.examManager.mapper.CourseMapper;
import com.genntii.examManager.mapper.ExamMapper;
import com.genntii.examManager.mapper.QuestionBankMapper;
import com.genntii.examManager.service.TestService;
import jakarta.annotation.Resource;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.UUID;

@Service
public class TestServiceImpl implements TestService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private ExamMapper examMapper;

    @Resource
    private CourseMapper courseMapper;

    @Resource
    private QuestionBankMapper questionBankMapper;

    @Override
    public void addQuestionBack() {
        //初始化题库
        QuestionBankMongo questionBankMongo = new QuestionBankMongo();
        Long uuidM = Long.valueOf(UUIDLong.getGuid());
        questionBankMongo.set_id(uuidM);
        questionBankMongo.setName("测试题库模板");
        questionBankMongo.setScore(1);
        questionBankMongo.setType((byte)1);

        ArrayList<Option> optionM = new ArrayList<>();
        for (int i = 0; i <= 3;i++){
            Option option = new Option(String.valueOf(i),i!=3);
            optionM.add(option);
        }

        // 多选题模板插入数据库
        MultipleChoice multipleChoice = MultipleChoice.builder()
                .score(1)
                .question("以下哪些项属于集合{0,1,2,4}")
                .options(optionM)
                .build();
        TopicMongo topic = TopicMongo.builder()
                .multipleChoice(multipleChoice)
                .build();

        questionBankMongo.setTopic(topic);
        QuestionBank questionBank = new QuestionBank(uuidM,"测试题库模板",(byte)1,1L);
        questionBankMapper.insert(questionBank);
        mongoTemplate.insert(questionBankMongo);

        questionBankMongo = new QuestionBankMongo();
        Long uuidS = Long.valueOf(UUIDLong.getGuid());
        questionBankMongo.set_id(uuidS);
        questionBankMongo.setName("测试题库模板");
        questionBankMongo.setScore(1);
        questionBankMongo.setType((byte)1);
        // 单选题插入数据库
        ArrayList<Option> optionS = new ArrayList<>();
        for (int i = 0; i <= 3;i++){
            Option option = new Option(String.valueOf(i),i==2);
            optionS.add(option);
        }
        SingleChoice singleChoice = SingleChoice.builder()
                .score(1)
                .question("1+1=?")
                .options(optionS)
                .build();
        topic = TopicMongo.builder()
                .singleChoice(singleChoice)
                .build();

        questionBankMongo.setTopic(topic);
        questionBank = new QuestionBank(uuidS,"测试题库模板",(byte)1,1L);
        questionBankMapper.insert(questionBank);
        mongoTemplate.insert(questionBankMongo);

        questionBankMongo = new QuestionBankMongo();
        Long uuidT = Long.valueOf(UUIDLong.getGuid());
        questionBankMongo.set_id(uuidT);
        questionBankMongo.setName("测试题库模板");
        questionBankMongo.setScore(1);
        questionBankMongo.setType((byte)1);
        //判断题
        TrueFalse trueFalse = TrueFalse.builder()
                .question("我帅不帅")
                .isCorrect(true)
                .score(1)
                .build();

        topic = TopicMongo.builder()
                .trueFalse(trueFalse)
                .build();

        questionBankMongo.setTopic(topic);
        questionBank = new QuestionBank(uuidT,"测试题库模板",(byte)1,1L);
        questionBankMapper.insert(questionBank);
        mongoTemplate.insert(questionBankMongo);

        questionBankMongo = new QuestionBankMongo();
        Long uuidV = Long.valueOf(UUIDLong.getGuid());
        questionBankMongo.set_id(uuidV);
        questionBankMongo.setName("测试题库模板");
        questionBankMongo.setScore(1);
        questionBankMongo.setType((byte)1);
        //简答题
        Vocabulary vocabulary = Vocabulary.builder()
                .score(1)
                .question("请介绍一下自己")
                .build();
        topic = TopicMongo.builder()
                .vocabulary(vocabulary)
                .build();
        questionBankMongo.setTopic(topic);
        questionBank = new QuestionBank(uuidV,"测试题库模板",(byte)1,1L);
        questionBankMapper.insert(questionBank);
        mongoTemplate.insert(questionBankMongo);

    }

    @Override
    public void addExam() {
        ArrayList<Option> optionM = new ArrayList<>();
        for (int i = 0; i <= 3;i++){
            Option option = new Option(String.valueOf(i),i!=3);
            optionM.add(option);
        }
        ArrayList<MultipleChoice> multipleChoices = new ArrayList<>();
        multipleChoices.add(MultipleChoice.builder()
                .score(25)
                .question("以下哪些项属于集合{0,1,2,4}")
                .options(optionM)
                .build());
        ArrayList<Option> optionS = new ArrayList<>();
        for (int i = 0; i <= 3;i++){
            Option option = new Option(String.valueOf(i),i==2);
            optionS.add(option);
        }
        ArrayList<SingleChoice> singleChoices = new ArrayList<>();
        singleChoices.add(SingleChoice.builder()
                .score(25)
                .question("1+1=?")
                .options(optionS)
                .build());
        ArrayList<TrueFalse> trueFalses = new ArrayList<>();
        trueFalses.add(TrueFalse.builder()
                .question("我帅不帅")
                .isCorrect(true)
                .score(25)
                .build());
        ArrayList<Vocabulary> vocabularies = new ArrayList<>();
        vocabularies.add(Vocabulary.builder()
                .score(25)
                .question("请介绍一下自己")
                .build());
        Topic topic = Topic.builder()
                .multipleChoices(multipleChoices)
                .singleChoices(singleChoices)
                .trueFalses(trueFalses)
                .vocabularies(vocabularies)
                .build();
        Exam exam = examMapper.selectById(1);
        ExamDetail examDetail = new ExamDetail();
        examDetail.set_id(1L);
        examDetail.setScore(100);
        examDetail.setContinueTime(1);
        examDetail.setStartTime(exam.getStartTime());
        examDetail.setEndTime(exam.getEndTime());
        examDetail.setCourseName("1");
        examDetail.setTeacherName("蔡博宇");
        examDetail.setSubjectName("Java程序设计");
        examDetail.setTopic(topic);
        mongoTemplate.insert(examDetail);
    }

    @Override
    public void addPractice() {
        ArrayList<Option> optionM = new ArrayList<>();
        for (int i = 0; i <= 3;i++){
            Option option = new Option(String.valueOf(i),i!=3);
            optionM.add(option);
        }
        ArrayList<MultipleChoice> multipleChoices = new ArrayList<>();
        multipleChoices.add(MultipleChoice.builder()
                .score(25)
                .question("以下哪些项属于集合{0,1,2,4}")
                .options(optionM)
                .build());
        ArrayList<Option> optionS = new ArrayList<>();
        for (int i = 0; i <= 3;i++){
            Option option = new Option(String.valueOf(i),i==2);
            optionS.add(option);
        }
        ArrayList<SingleChoice> singleChoices = new ArrayList<>();
        singleChoices.add(SingleChoice.builder()
                .score(25)
                .question("1+1=?")
                .options(optionS)
                .build());
        ArrayList<TrueFalse> trueFalses = new ArrayList<>();
        trueFalses.add(TrueFalse.builder()
                .question("我帅不帅")
                .isCorrect(true)
                .score(25)
                .build());
        ArrayList<Vocabulary> vocabularies = new ArrayList<>();
        vocabularies.add(Vocabulary.builder()
                .score(25)
                .question("请介绍一下自己")
                .build());
        Topic topic = Topic.builder()
                .multipleChoices(multipleChoices)
                .singleChoices(singleChoices)
                .trueFalses(trueFalses)
                .vocabularies(vocabularies)
                .build();

        Course course = courseMapper.selectById(1);
        ExamDetail examDetail = new ExamDetail();
        examDetail.set_id(1L);
        examDetail.setScore(100);
        examDetail.setContinueTime(1);
        examDetail.setCourseName("1");
        examDetail.setTeacherName("蔡博宇");
        examDetail.setSubjectName("Java程序设计");
        examDetail.setTopic(topic);
        mongoTemplate.insert(examDetail,"practiceDetail");
    }
}
